package com.etoc.test.flow.kd;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import com.etoc.test.util.DateUtil;
import com.etoc.test.util.EncryptUtil;
import com.etoc.test.util.HttpUtil;

/**
 * ��������ʵ����
 * <һ�仰���ܼ���>
 * <������ϸ����>
 * 
 * @author  �³�
 * @version  [�汾��, 2015-8-10]
 * @see  [�����/����]
 * @since  [��Ʒ/ģ��汾]
 */
public class OrderProc implements Serializable {
	/**
	 * ע������
	 */
	private static final long serialVersionUID = -3277728109468720716L;
	private String action;
	private String appKey;
	private String appSecret;
	private String orderId;
	private String timeStamp;
	private String sign;
	Map<String, String> map = new TreeMap<String, String>();

	public OrderProc() {
		super();
	}

	public OrderProc(String action, String appKey, String appSecret, String orderId, String timeStamp, String sign) {
		super();
		this.action = action;
		this.appKey = appKey;
		this.appSecret = appSecret;
		this.orderId = orderId;
		this.timeStamp = timeStamp;
		this.sign = sign;
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	public String getAppKey() {
		return appKey;
	}

	public void setAppKey(String appKey) {
		this.appKey = appKey;
	}

	public String getAppSecret() {
		return appSecret;
	}

	public void setAppSecret(String appSecret) {
		this.appSecret = appSecret;
	}

	public String getOrderId() {
		return orderId;
	}

	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}

	public String getTimeStamp() {
		return timeStamp;
	}

	public void setTimeStamp(String timeStamp) {
		this.timeStamp = timeStamp;
	}

	public String getSign() {
		return sign;
	}

	public void setSign(String sign) {
		this.sign = sign;
	}
	
	public Map<String, String> getMap() {
		return map;
	}
	
	public String signString() {
		if (this.action != null && !this.action.equals(""))
		{
			map.put("action", this.action);
		}
		if (this.appKey != null && !this.appKey.equals(""))
		{
			map.put("appKey", this.appKey);
		}
		if (this.orderId != null && !this.orderId.equals(""))
		{
			map.put("orderId", this.orderId);
		}
		if (this.timeStamp != null && !this.timeStamp.equals(""))
		{
			map.put("timeStamp", this.timeStamp);
		}
		
		Iterator<String> keySet = map.keySet().iterator();
		StringBuffer signString = new StringBuffer(this.appSecret);
		while (keySet.hasNext())
		{
			String key = keySet.next();
			String val = map.get(key);
			signString.append(key).append(val);
		}
		signString.append(this.appSecret);
		return signString.toString(); 
	}

	@Override
	public String toString() {
		Iterator<String> keySet = map.keySet().iterator();
		String ret = "";
		while (keySet.hasNext())
		{
			String key = keySet.next();
			String val = map.get(key);
			ret += key + "=" + val + "&";
		}
		ret += "sign" + "=" + sign;
		return ret;
	}
	
	public static void main(String[] args)
	{
		OrderProc proc = new OrderProc();
		proc.setAction("orderProc");
		proc.setAppKey("4aea47a6465e3de901465e68c8100000");
		proc.setAppSecret("unicom");
		proc.setOrderId("1603040210274001");
		proc.setTimeStamp(DateUtil.dateToStr(new Date(), "yyyyMMddHHmmss"));
		String sign = proc.signString();
		proc.setSign(EncryptUtil.encryptSHA(sign));
		
		String url = "http://61.50.245.139/diyAgent/tencentQQ";
		System.out.println(url + "?" + proc.toString());
		//http://61.50.245.139/flowAgent
		//http://100.0.4.8:8080/flowAgent
		//http://113.57.243.18:8070/diyAgent/tencentQQ
		
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		Map<String, String> map = proc.getMap();
		Iterator<String> keySet = map.keySet().iterator();
		while (keySet.hasNext())
		{
			String key = keySet.next();
			String val = map.get(key);
			
			nvps.add(new BasicNameValuePair(key, val));
		}
		nvps.add(new BasicNameValuePair("sign", proc.getSign()));
		
		long startTime = System.currentTimeMillis();
		String respCode = HttpUtil.httpRequest(url, nvps);
		long endTime = System.currentTimeMillis();
		System.out.println("orderProc=" + respCode);
		System.out.println("spend " + (endTime - startTime) + " milli second.");
	}
	
}

